import axios from 'axios'
import { message } from 'antd'
import store from '../store'
import { toLoginWithFrom } from './common'

const request = axios.create({
  timeout: 6000,
})
// 请求拦截器
request.interceptors.request.use((config) => {
  config.headers.token = store.getState()?.authority?.token || ''
  return config
}, (error) => Promise.reject(error))

// 响应拦截器
request.interceptors.response.use((res) => {
  if (res.data.code === '200') {
    if (res.config.method !== 'get') {
      if (!res.config.silent) {
        message.success(res.data.message)
      }
    }
    // console.log(res.data)
    return res.config.fullRes ? res.data : res.data.data
    // return res.data.data
  }
  if (res.data.code === '10119') {
    toLoginWithFrom()
  }
  message.error(res.data.message)
  throw new Error(res.data.message)
}, (error) => Promise.reject(error))

export default request;
